package geon.dao;

import geon.model.Site;
import geon.model.SiteTipo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DaoSite implements Dao<Site> {
	
	private DaoGenerico dao;
	private String sql;

	public DaoSite() {
		dao = new DaoGenerico();
	}

	@Override
	public Integer add(Site o) throws SQLException {
		try {
			dao.begin();
			sql = "Insert Into TbSite (SitCodigo, SitTipo, SitUrl) Values (?, ?, ?)";
			o.setCodigo(dao.getNextId("TbSite", "SitCodigo"));
			dao.executeCommand(sql, o.getCodigo(), o.getTipo().toString(), o.getUrl());
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
		return o.getCodigo();
	}

	@Override
	public void upd(Site o) throws SQLException {
		try {
			dao.begin();
			sql = "Update TbSite Set SitTipo = ?, SitUrl = ? Where SitCodigo = ?";
			dao.executeCommand(sql, o.getTipo().toString(), o.getUrl(), o.getCodigo());
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
	}

	@Override
	public void del(int codigo) throws SQLException {
		try {
			dao.begin();
			sql = "Delete From TbSite Where SitCodigo = ?";
			dao.executeCommand(sql, codigo);
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
	}

	@Override
	public Site set(ResultSet rs) throws SQLException {
		Site site = new Site();
		site.setCodigo(rs.getInt("SitCodigo"));
		site.setTipo(SiteTipo.valueOf(rs.getString("SitTipo")));
		site.setUrl(rs.getString("SitUrl"));
		return site;
	}

	@Override
	public Site get(int codigo) throws SQLException {
		Site site = null;
		sql = "Select SitCodigo, SitTipo, SitUrl From TbSite Where SitCodigo = ?";
		ResultSet rs = dao.executeQuery(sql, codigo);
		if (rs.next()) {
			site = set(rs);
		}
		rs.close();
		return site;
	}
	
	@Override
	public Site get(Object param) throws SQLException {
		Site site = null;
		sql = "Select SitCodigo, SitTipo, SitUrl From TbSite Where SitUrl = ?";
		ResultSet rs = dao.executeQuery(sql, param);
		if (rs.next()) {
			site = set(rs);
		}
		rs.close();
		return site;
	}

	public List<Site> list() throws SQLException {
		List<Site> sites = new LinkedList<Site>();
		sql = "Select SitCodigo, SitTipo, SitUrl From TbSite";
		ResultSet rs = dao.executeQuery(sql);
		while (rs.next()) {
			sites.add(set(rs));
		}
		rs.close();
		return sites;
	}

	@Override
	public List<Site> list(Object... params) throws SQLException {
		List<Site> sites = new LinkedList<Site>();
		sql = "Select SitCodigo, SitTipo, SitUrl From TbSite Where SitUrl Like ?";
		ResultSet rs = dao.executeQuery(sql, "%" + params[0] + "%");
		while (rs.next()) {
			sites.add(set(rs));
		}
		rs.close();
		return sites;
	}


}
